package com.simon.hse.fragment;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.LoginActivity;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.ApiConstanse;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.UserInfoOutput;
import com.simon.hse.update.UpdateBean;
import com.simon.hse.update.UpdateVersion;
import com.simon.hse.utils.ToastUtils;

import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import retrofit2.Call;

/**
 * 我的
 */
@SuppressWarnings("all")
public class MineFragment extends BaseFragment {
    @ViewInject(R.id.iv_mine_avatar)
    ImageView mIv_mine_avatar;

    @ViewInject(R.id.tv_mine_name)
    TextView mTv_mine_name;

    @ViewInject(R.id.tv_mine_factory)
    TextView mTv_mine_factory;

    private UpdateBean updateBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initUserInfo();
        initVersion();
    }

    private void initVersion() {
        updateBean = new UpdateBean();
        updateBean.setTitle("版本更新");
        updateBean.setMessage("版本更新详情!");
        updateBean.setVersionCode(1);
        updateBean.setVersionName("1.0.2");
        updateBean.setUrl("http://apps.lanhai.pro/android/lanhaifinance-1.2.3.apk");
    }

    @Event(value = {R.id.ll_check_version, R.id.rl_check_version})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.ll_check_version:
                UpdateVersion.CheckVersion(getContext(), updateBean);
                break;
            case R.id.rl_check_version:
                startActivity(new Intent(getContext(), LoginActivity.class));
                break;
        }
    }

    /**
     * 用户信息
     */
    private void initUserInfo() {
        Call<String> call = HttpManager.getInstance().requestService().LOGIN_DETAIL();
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                Log.i("Simon", "result = " + result);
                UserInfoOutput userInfoOutput = new Gson().fromJson(result, UserInfoOutput.class);
                if (userInfoOutput.getResult().equals("Success")) {
                    final UserInfoOutput.DataBean dataBean = userInfoOutput.getData();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTv_mine_name.setText(dataBean.getName());
//                            List<String> sysDepartmentName = dataBean.getSysDepartmentName();
//                            StringBuffer sb = new StringBuffer();
//                            for (String s : sysDepartmentName) {
//                                sb.append(s + " ");
//                            }
                            mTv_mine_factory.setText(dataBean.getWorkCompanyText() + " " + dataBean.getSysDepartmentName());

                            String imageUrl = dataBean.getPhoneID();
                            if (imageUrl != null && !imageUrl.equals("") && !imageUrl.equals("00000000-0000-0000-0000-000000000000")) {
                                ImageOptions imageOptions = new ImageOptions.Builder()
                                        .setFailureDrawableId(R.mipmap.account_pic)
                                        .setUseMemCache(true)
                                        .setCircular(true) // 设置成圆形图片
                                        .setFadeIn(true) //淡入效果
                                        .setImageScaleType(ImageView.ScaleType.FIT_CENTER)
                                        .build();
                                x.image().bind(mIv_mine_avatar, ApiConstanse.BASE_URL + ApiConstanse.DOWNLOAD_FILE + "?id=" + dataBean.getId(), imageOptions);
                            }
                        }
                    });
                } else {
                    ToastUtils.getInstance().showShortToastBottom(userInfoOutput.getMessage().toString());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1000) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                UpdateVersion.CheckVersion(getContext(), updateBean);
            } else {
                Toast.makeText(getContext(), "请在应用管理中打开“存储权限”访问权限,否则无法正常使用！", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
